//super jumping! jumping! jumping!
#include <iostream>
#include <vector>
using namespace std;

vector<long long> quene;
vector<vector<long long>> sequence;
vector<long long> temp;
int main()
{
    ios::sync_with_stdio(false);
    int n, tmp, i, j, k, m;
    long long maxsum, tmpsum;
    bool added;
    while (cin >> n)
    {
        maxsum = 0;
        tmpsum = 0;
        if (!n)
        {
            break;
        }
        for (i = 0; i < n; i++)
        {
            cin >> tmp;
            quene.push_back(tmp);
        }
        for (i = 0; i < n; i++)
        {
            added = false;
            j = sequence.size();
            while (j--)
            {
                if (quene[i] > sequence[j].back())
                {
                    sequence[j].push_back(quene[i]);
                    added = true;
                }
            }
            if (!added)
            {
                temp.push_back(quene[i]);
                sequence.push_back(temp);
                temp.clear();
            }
        }
        k = sequence.size();
        while (k--)
        {
            m = sequence[k].size();
            tmpsum = 0;
            for (i = 0; i < m; i++)
            {
                tmpsum += sequence[k][i];
            }
            if (tmpsum > maxsum)
            {
                maxsum = tmpsum;
            }
        }
        cout << maxsum << endl;
        quene.clear();
        sequence.clear();
    }
    return 0;
}